package com.example.tse.rbhttpservice.service.member;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberFavoriteGetList;
import com.example.tse.rbhttpservice.service.member.model.MemberFavoriteGetListRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberFavoriteGetListResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;





/**
 *
 * @author wliu
 *    edi  tse
 */
public class MemberFavoriteServiceImpl implements MemberFavoriteService {
	private Context context;
	private static Gson gson = new Gson();


	public MemberFavoriteServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 获取用户个人的收藏商品列表，返回结果按照添加时间到序，当某商品已下架时显示“已失效”
	 * @param request
	 * @param listener
	 * @return MemberFavoriteGetListResponse
	 */

	    public void getList(MemberFavoriteGetListRequest request, final RemoteServiceListenerT< MemberFavoriteGetListResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "member/favorite/getList";



        /**
        favorite
        getList
        GetList
        获取用户个人的收藏商品列表，返回结果按照添加时间到序，当某商品已下架时显示“已失效”
        GET
        MemberFavorite
        MemberFavoriteGetList
        member/favorite/getList
        array
        */

           final Type type = new TypeToken<List<MemberFavoriteGetList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MemberFavoriteGetListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MemberFavoriteGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MemberFavoriteGetListResponse responce = new MemberFavoriteGetListResponse();
                 responce.setGetList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MemberFavoriteGetListResponse());
                 } else {
                     List<MemberFavoriteGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberFavoriteGetListResponse responce = new MemberFavoriteGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MemberFavoriteGetListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MemberFavoriteGetListResponse(), fromWhere);
                 } else {
                     List<MemberFavoriteGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberFavoriteGetListResponse responce = new MemberFavoriteGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MemberFavoriteGetListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }

             @Override
             public void onMessage(String message) {
                 super.onMessage(message);
                 listener.onMessage(message);
             }


         });
    }






}